// pages/admin_z/admin_z.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uList: [],
    type: '',
    name: '',
    factoryGroup:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    switch (options.type) {
      case "设计师":
        this.data.name = "设计师"
        break
      case "业务经理":
        this.data.name = "业务员"
        break
      case "采购经理":
        this.data.name = "采购员"
        break
      case "产品经理":
        this.data.name = "工厂账号"
        break
      case "仓库经理":
        this.data.name = "仓库"
        break
      case "子管理员":
        this.data.name = "子管理员"
        break
    }
    this.setData({
      type: options.type,
      name: this.data.name
    })
    
  },

  userList: function() {
    var that = this
    wx.request({
      url: app.globalData.api + 'user/list',
      data: {
        page: 1,
        rows: 999,
        roleName: this.data.type
      },
      method: 'POST',
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: function(res) {
        console.log(res)
        if (res.data.code == 200) {
          that.setData({
            uList: res.data.data.userList
          })
          if (that.data.type == '产品经理') {
            that.getFactoryGroup()
          }
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.userList()
    
  },
  addAccount: function() {
    wx.navigateTo({
      url: '/pages/account/account?type=' + this.data.type
    })
  },
  edlitUser: function(e) {
    wx.navigateTo({
      url: '/pages/account/account?type=' + this.data.type + '&id=' + e.currentTarget.dataset.id,
    })
  },
  deleUser: function (e) {
    var that = this
    wx.showModal({
      title: '提示',
      content: '确认删除?',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.api + 'user/' + e.currentTarget.dataset.id,
            data: {},
            header: {
              Authorization: wx.getStorageSync('token')
            },
            method: 'DELETE',
            success: (res) => {
              if (res.data.code == 200) {
                wx.showToast({
                  title: '删除成功',
                })
                that.userList()
              }
            }
          })
        }
      }
    })
  },
  getFactoryGroup() {
    var that = this
    wx.request({
      url: app.globalData.api + 'factoryGroup/list',
      data: {
        rows: 99
      },
      method: 'POST',
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success(res) {
        console.log(res)
        if (res.data.code == 200) {
          for (let i of res.data.data.factoryGroupList) {
            for (let j of that.data.uList){
              if (i.id == j.factoryGroupId){
                that.data.factoryGroup.push(i)
              }
            }
          }
          console.log(that.data.factoryGroup)
          that.setData({
            factoryGroup: that.data.factoryGroup
          })
        }
      }
    })
  },
})